home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 001-025 / disk_002 / make2 / make.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  8KB  |  281 lines

  1. #include "rules.h"
  2. #include "vars.h"
  3. #include <stdio.h>
  4. #include <libraries/dos.h>
  5.  
  6. int debug;
  7. long int days, mins, secs;
  8.  
  9. main( argc, argv )
  10.    int argc;
  11.    char **argv;
  12. {
  13.    FILE *MakeFile;
  14.    int i,j;
  15.  
  16.    printf("Make V1.0 (c) 1985 by General Overall Design.\n");
  17.    printf("May (only) be distributed free of charge.\n");
  18.    /* General Overall Design
  19.     * P.O. Box 2039
  20.     * W. Lavayette IN 47906
  21.     */
  22.    if( argc > 2 && strcmp(argv[1], "-f")==0 ){       /* make -f file ... */
  23.       MakeFile = fopen( argv[2] , "r" );
  24.       argc -= 2;
  25.       argv += 2;
  26.    } else
  27.       MakeFile = fopen("makefile" , "r");
  28.  
  29.    if( argc > 1 && strcmp( argv[1], "-d" ) == 0 ){
  30.       debug=1;
  31.       argc--;
  32.       argv++;
  33.       printf("debug on.\n");
  34.    }
  35.  
  36.    if( MakeFile == NULL ){                  /* open failed  */
  37.       printf("make: can't open makefile\n");
  38.       exit( 1 );
  39.    }
  40.    if ( debug )
  41.       printf("makefile open\n");
  42.  
  43.    Parse( MakeFile );
  44.  
  45.    if( debug ){
  46.       printf("Variables:\n");
  47.       for( i=0 ; i < n_Vars ; i++ )
  48.          printf( "%s = '%s'\n", avr_Vars[i].pch_name , avr_Vars[i].string );
  49.       printf("Rules:\n");
  50.       for( i=0 ; i < n_Rules ; i++ ){
  51.          printf( "%s : ", arl_Rules[i].pch_target );
  52.          for( j = 0; j < arl_Rules[i].c_depend; j++ )
  53.             printf("'%s' ", arl_Rules[i].apch_depend[ j ] );
  54.          printf("\n");
  55.          for( j = 0; j < arl_Rules[i].c_action; j++ )
  56.             printf("%s\n", arl_Rules[i].apch_action[j] );
  57.       }
  58.    }
  59.  
  60.    if( argc == 1 )
  61.       make( arl_Rules[0].pch_target, &days, &mins, &secs );
  62.    else
  63.       while( --argc != 0 )
  64.          make( *++argv, &days, &mins, &secs );
  65.  
  66.    printf("done.\n");
  67. }
  68.  
  69. static char *GetLine(), *NextWord(), *malloc();
  70.  
  71. Parse( fd )
  72.     FILE *fd;
  73. {
  74.     char *pch_Position, *pch_Word, *pch_Target;
  75.  
  76.     pch_Position = GetLine( fd );
  77.     while( pch_Position ){
  78.         if( debug )
  79.            printf("Parsing:%s\n", pch_Position );
  80.         if( '#' == *pch_Position ){
  81.             pch_Position = GetLine(fd);
  82.             continue;
  83.         }
  84.         pch_Word = pch_Position;
  85.         pch_Position = NextWord( pch_Position );
  86.         if( *pch_Position == '=' ){
  87.             DefineVar( pch_Word, ++ pch_Position );
  88.             pch_Position = GetLine( fd );
  89.         } else {
  90.             pch_Target = pch_Word;
  91.             pch_Word = pch_Position = NextWord( pch_Position );
  92.             pch_Position = NextWord( pch_Position );
  93.             while( pch_Word ){
  94.                  AddDependant( pch_Word, pch_Target );
  95.                  pch_Word = pch_Position;
  96.                  pch_Position = NextWord( pch_Position );
  97.             }
  98.             pch_Position = GetLine( fd );
  99.             while( ' ' == *pch_Position ){
  100.                  AddAction( pch_Position , pch_Target );
  101.                  pch_Position = GetLine( fd );
  102.             }
  103.         }
  104.     }
  105. }
  106.  
  107. char *
  108. NextWord( pch )
  109.     char *pch;
  110. {
  111.     if( NULL == pch || NULL == *pch )
  112.         return NULL;
  113.     while( *pch && *pch != ' ' && *pch != '\t' && *pch != ',' )
  114.         pch++;
  115.     if( *pch == '\0' )
  116.         return NULL;
  117.     else
  118.         *pch = '\0';
  119.     do
  120.         pch++;
  121.     while ( *pch && (' ' == *pch || '\t' == *pch || ',' == *pch ));
  122.     if(*pch == '\0')
  123.         return NULL;
  124.     else
  125.         return pch;
  126. }
  127.  
  128. /* states for GetLine */
  129. #define HALT        -1
  130. #define IGNOREWHITE  0
  131. #define START        1
  132. #define NONWHITE     2
  133. #define BACKSLASH    3
  134. #define VARIABLE     4
  135. char *names[]={"ignorewhite","start","nonwhite","backslash","variable"};
  136.  
  137. static char *
  138. GetLine( fd )
  139.    FILE *fd;
  140. {
  141.    static char buf[ 1024 ];
  142.    auto int pos = 0;
  143.    auto int state = START;
  144.    int ch;
  145.    char *pch_var, *pch_replace;
  146.  
  147.    ch = getc(fd);
  148.    if( EOF == ch )
  149.       return NULL;
  150.    while( state != HALT ){
  151.        if( debug )
  152.           printf( "state: %s\n", names[state]);
  153.        switch( state ){
  154.        case IGNOREWHITE:
  155.                   if ( ' ' == ch || '\t' == ch || ',' == ch )
  156.                       ch = getc(fd);
  157.                   else
  158.                       state = START;
  159.                   break;
  160.        case START:
  161.                   if( ' ' == ch || '\t' == ch || ',' == ch ) {
  162.                       buf[pos++] = ch;
  163.                       ch = getc(fd);
  164.                       state = IGNOREWHITE;
  165.                       break;
  166.                   } else if( '$' == ch ) {
  167.                       state = VARIABLE;
  168.                       ch = getc(fd);
  169.                       break;
  170.                   } else if( '\\' == ch ) {
  171.                       ch = getc(fd);
  172.                       state = BACKSLASH;
  173.                       break;
  174.                   } else if( '\n' == ch || EOF == ch ) {
  175.                       state = HALT;
  176.                       break;
  177.                   } else {
  178.                       state = NONWHITE;
  179.                       break;
  180.                   }
  181.      case NONWHITE :
  182.                  if( ch != EOF && ch != '\n' && ch != '$' &&
  183.                      ch != '\\' && ch != ' ' && ch != '\t' && ch != ',' ) {
  184.                       buf[ pos++ ] = ch;
  185.                       ch = getc(fd);
  186.                  } else
  187.                       state = START;
  188.                  break;
  189.      case BACKSLASH:
  190.                  if( ch != '\n' ){
  191.                     buf[pos++] = '\\';
  192.                     buf[pos++] = ch;
  193.                  }
  194.                  ch = getc(fd );
  195.                  state = START;
  196.                  break;
  197.      case VARIABLE:
  198.                  if( '$' == ch ) {
  199.                     buf[pos++] = ch;
  200.                     state = START;
  201.                  } else if ( '{'== ch || '(' == ch ) {
  202.                     ch = getc(fd);
  203.                     pch_var = &buf[ pos ];
  204.                     while ( ')' != ch && '}' != ch ){
  205.                          buf[ pos++ ] = ch;
  206.                          ch = getc(fd);
  207.                     }
  208.                     ch = getc(fd);
  209.                     buf[pos]=NULL;
  210.                     pch_replace = VarLookup( pch_var );
  211.                     strcpy( pch_var , pch_replace );
  212.                     pos = strlen( buf );
  213.                     state = START;
  214.                  }
  215.                  break;
  216.          }
  217.      }
  218.      buf[pos] = NULL;
  219.      if( debug )
  220.         printf("buf='%s'\n", buf );
  221.      pch_replace = malloc( strlen(buf) + 1 );
  222.      if( pch_replace == NULL ){
  223.         printf("make: out of memory" );
  224.         exit(1);
  225.      }
  226.      strcpy( pch_replace , buf );
  227.      return pch_replace;
  228. }
  229.  
  230. make( pch_Target, mydays, mymins, mytics )
  231.    char *pch_Target;
  232.    long int *mydays, *mymins, *mytics;
  233. {
  234.    long int hisdays, hismins, histics;
  235.    int fl_Action;
  236.    char **depends, **actions;
  237.  
  238.    GetDate( pch_Target, mydays, mymins, mytics );
  239.    if( debug )
  240.       printf("make(): %s %ld %ld:%ld\n",pch_Target,*mydays,*mymins,*mytics );
  241.    depends = GetDependants( pch_Target );
  242.    actions = GetActions( pch_Target );
  243.    fl_Action = *mydays == 0 && *mymins == 0 && *mytics == 0;
  244.    while( *depends ){
  245.       if( make( *depends++, &hisdays, &hismins, &histics ) )
  246.          fl_Action = 1;
  247.       if( hisdays > *mydays || (hisdays == *mydays &&
  248.           ( hismins > *mymins || (hismins == *mymins && histics > *mytics ))))
  249.          fl_Action = 1;
  250.    }
  251.    if( fl_Action )
  252.       while( *actions ) {
  253.           printf("%s\n", *actions );
  254.           Execute( *actions, 0, 0 );
  255.           actions++;
  256.       }
  257.    return fl_Action;
  258. }
  259.  
  260. GetDate( pch_FileName, days, mins, secs )
  261.    char *pch_FileName;
  262.    long int *days, *mins, *secs;
  263. {
  264.    static struct FileInfoBlock buf;
  265.    char *p;
  266.  
  267.    if( p = (char *)Lock( pch_FileName, ACCESS_READ )){
  268.       if( Examine( p , &buf ) ) {
  269.          *days = buf.fib_Date.ds_Days;
  270.          *mins = buf.fib_Date.ds_Minute;
  271.          *secs = buf.fib_Date.ds_Tick;
  272.       } else
  273.          printf("Couldn't get info for %s\n", pch_FileName );
  274.       UnLock( p );
  275.    } else {
  276.       if ( debug )
  277.          printf("couldn't lock %s\n", pch_FileName );
  278.       *days = *mins = *secs = 0;
  279.    }
  280. }
  281.